<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
        // 使用贪心的思想来解决，对贪心先有一个感性的认识
        // 这种写法总感觉很傻
        var maxProfit = function(prices) {
            let profit = 0;
            let max = 0;
            let p = 0;
            let q = 1;
            while (q <= prices.length) {
                if (prices[p] > prices[q]) {
                    profit += max;
                    max = 0;
                    p = q;
                    q = p + 1;
                } else {
                    let tmp = prices[q] - prices[p];
                    if (tmp > max) {
                        max = tmp;
                        q++;
                    } else {
                        profit += max;
                        max = 0;
                        p = q;
                        q = p + 1;
                    }
                    
                }
            }
            return profit;
        };
        console.log(maxProfit([1, 2, 3, 4, 5]));
    </script>
</body>
</html>